package main

import (
	"crypto/md5"
	"encoding/csv"
	"encoding/hex"
	"fmt"
	"os"
)

type CsvExporter struct {
	writer   *csv.Writer
	filename string
	file     os.File
}

func NewCsvExport(title string) *CsvExporter {
	filename :=  "mytest.csv"
	file, _ := os.OpenFile("./"+filename, os.O_CREATE|os.O_WRONLY, 0777)

	csvWriter := csv.NewWriter(file)

	_, _ = file.WriteString("\xEF\xBB\xBF")

	return &CsvExporter{
		writer:   csvWriter,
		filename: filename,
	}
}

func (e *CsvExporter) WriteAll(content [][]string) error {
	return e.writer.WriteAll(content)
}

func (e *CsvExporter) Write(record []string) error {
	return e.writer.Write(record)
}

func (e *CsvExporter) GetFileName() string {
	e.writer.Flush()
	_ = e.file.Close()
	return e.filename
}

func Md5(origin string) string {
	bytes := []byte(origin)
	h := md5.New()
	h.Write(bytes) // 需要加密的字符串为 123456
	cipherStr := h.Sum(nil)
	return hex.EncodeToString(cipherStr) // 输出加密结果
}

func main() {
	exportor := NewCsvExport("测试")
	data := [][]string{
		{
			"第一行",
			"第一行",
		},
		{
			"第一行数据",
			"第一行数据",
		},
	}
	err := exportor.WriteAll(data)
	fmt.Println(err)
}
